package main.java.LeetCode.ClassicalAlgorithm.Worker.Impl.event;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class FutureResult<E>{
    private final CountDownLatch countDownLatch = new CountDownLatch(1);

    private E result;

    public void setResult(E result){
        this.result = result;
        countDownLatch.countDown();
    }

    public E getResult() throws InterruptedException {
        countDownLatch.await();
        return result;
    }

    public E getResult(long timeout, TimeUnit unit) throws InterruptedException {
        boolean await = countDownLatch.await(timeout, unit);
        if(await)
            return result;
        else
            return null;
    }

    public boolean isDone(){
        return countDownLatch.getCount()==0;
    }
}
